package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.service.DtsOrderGoodsService;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAndUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 11:35
 * description:
 */

@Service
public class DtsOrderGoodsServiceImpl implements DtsOrderGoodsService {

    @Autowired
    private DtsOrderGoodsMapper orderGoodsMapper;

    @Autowired
    private StatMapper statMapper;

    @Override
    public int findByGoodsId(Integer id) {
        LambdaQueryWrapper<DtsOrderGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsOrderGoods::getGoodsId,id);
        wrapper.eq(DtsOrderGoods::getDeleted,false);
        Integer count = orderGoodsMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public List<OrderAndUser> findOrderUser(int day) {
        List<DayStatis> userAmts = statMapper.findOrderAmt(30);
        List<DtsOrder> orderAndUser = orderGoodsMapper.findOrderAndUser(day);
        List list1 = new ArrayList<>();
        List<OrderAndUser> list = new ArrayList<>();
        for(int j = 0;j < userAmts.size();j++) {
            int i = 0;
            OrderAndUser ou = new OrderAndUser();
            for (DtsOrder orderU : orderAndUser) {
                if (userAmts.get(j).getDayStr() == null) {
                    ou.setDay(list.get(j - 1).getDay());
                    ou.setAmount(new BigDecimal(0));
                    ou.setOrders(0);
                    ou.setPcr(new BigDecimal(0));
                    ou.setCustomers(0);
                } else {
                    if (userAmts.get(j).getDayStr().equals(orderU.getMobile())) {
                        if (userAmts.get(j).getAmts() == null) {
                            userAmts.get(j).setAmts(new BigDecimal(0));
                            ou.setAmount(new BigDecimal(0));
                        } else {
                            ou.setAmount(userAmts.get(j).getAmts());
                        }
                        ou.setOrders(userAmts.get(j).getCnts());
                        if (orderU.getUserId() != null) {
                            if (list1.contains(orderU.getUserId())) {
                                ou.setCustomers(i);
                            } else {
                                ou.setCustomers(++i);
                            }
                            list1.add(orderU.getUserId());
                        }
                        ou.setDay(userAmts.get(j).getDayStr());
                        if (userAmts.get(j).getAmts().compareTo(BigDecimal.ZERO) == 0
                                || i == 0) {
                            ou.setPcr(new BigDecimal(0));
                        } else {
                            ou.setPcr(userAmts.get(j).getAmts().divide(new BigDecimal(i), 2, BigDecimal.ROUND_HALF_UP));
                        }
                    }
                }
            }
            list.add(ou);
            list1.clear();
        }
        return list;
    }
}
